home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 24
/
Amiga Format AFCD24 (Feb 1998, Issue 108).iso
/
-in_the_mag-
/
emulation
/
-otherstuff-
/
amiganpi
/
src_blitz
/
fram.asc
< prev
next >
Wrap
Text File
|
1998-01-05
|
4KB
|
222 lines
; frame handling
#SYN=$16
#DLE=$10
#STX=$02
#ETX=$03
Function read_frame{buff.l}
;buff is the address to a buffer
;return -1 if frame error
;return -2 (#TIMEOUT) if timeout (at current time, this could also signal an abort)
timeout=False
buf.w=#SYN ; a byte type will not return `-1'
i=0 ; a `-1' is used by GetByte to indicate timeout
org.l=buff; ; a `-2' is used by GetByte to indicate abort
fcs.l=0; ; currently timeouts and aborts are not differentiated
fcs_rec.l=0;
fcs_rec2.l=0;
While buf=#SYN
buf=GetByte{}
Wend
If buf=#DLE
buf=GetByte{}
If buf=#STX
Repeat
buf=GetByte{}
If buf<0
timeout=True
Else
If buf=#DLE
buf=GetByte{}
Select buf
Case -1
timeout=True
Case -2
timeout=True
Case #ETX
fcscalc{&fcs,Chr$(buf)}
fcs_rec=GetByte{}
If fcs_rec<0
timeout=True
Else
fcs_rec2=GetByte{}
If fcs_rec2<0
timeout=True
Else
fcs_rec=(fcs_rec2 ASL 8)+fcs_rec
EndIf
EndIf
If fcs_rec&$ffff=fcs&$ffff Then Function Return i
Notify_BadCRC{Right$(Hex$(fcs_rec),4),Right$(Hex$(fcs),4)}
Function Return -1
Case #DLE
Poke.b buff,buf
buff+1
fcscalc{&fcs,Chr$(buf)}
i+1
Default
Notify_BadFrame{}
Function Return -1
End Select
Else
Poke.b buff,buf
buff+1
fcscalc{&fcs,Chr$(buf)}
i+1
EndIf
EndIf
Until timeout=True ; repeat forever, unless timeout
EndIf
Else
If buf<0 Then timeout=True
EndIf
; could be a timeout error, or some other error
If timeout=True
Function Return #TIMEOUT
Else
Function Return -1
EndIf
End Function
Statement send_frame{buff.l,size.l}
;buff is a pointer to a series of bytes
fcs.l=0
WriteSerial 0,#SYN
WriteSerial 0,#DLE
WriteSerial 0,#STX
For i=0 To size-1
WriteSerial 0,Peek.b(buff+i)&$ff
If Peek.b(buff+i)=#DLE Then WriteSerial 0,#DLE
fcscalc{&fcs,Chr$(Peek.b(buff+i)&$ff)}
Next i
WriteSerial 0,#DLE
WriteSerial 0,#ETX
fcscalc{&fcs,Chr$(#ETX)}
WriteSerial 0,fcs&$ff
WriteSerial 0,(fcs&$ff00) ASR 8
End Statement
#LR=1
#LD=2
#LT=4
#LA=5
#LN=6
#LNA=7
Statement sendLA{nr,cr}
la$=Chr$(3)+Chr$(5)+Chr$(0)+Chr$(0)
Poke.b &la$+2,nr
Poke.b &la$+3,cr
send_frame{&la$,Len(la$)}
End Statement
s.w=0
r.w=0
Function sendData{d.l,size.l}
;data is a pointer to a string of bytes
;return -2 (#TIMEOUT) if timeout
SHARED s,r
timeout=False
buf$=String$(" ",2048)
buf2$=String$(" ",2048)
i.l=0
Poke.b &buf$,2
Poke.b &buf$+1,4
Poke.b &buf$+2,s
For i=0 To size-1
Poke.b &buf$+i+3,Peek.b(d+i)
Next i
Repeat
send_frame{&buf$,size+3}
dummy.l=read_frame{&buf2$}
If dummy<-1 Then timeout=True
If (Peek.b(&buf2$)=3) & (Peek.b(&buf2$+1)=5) & ((Peek.b(&buf2$+2)&$ff)=s)
s=(s+1)&$ff
Function Return size
Else
Notify_NewtonRejectsFrame{}
Else
EndIf
Until timeout=True ; repeat forever, unless timeout
Function Return #TIMEOUT
End Function
Function receiveData{d.l,size.l}
;data is a pointer to a string of bytes
;return -2 (#TIMEOUT) if timeout
SHARED r
buf$=String$(" ",2048)
ss.l=0
i.l=0
Repeat
ss=read_frame{&buf$}
If ss>0
sendLA{r,1}
r=(r+1)&$ff
If ss>size Then ss=size
For i=0 To ss-1
Poke.b d+i,Peek.b(&buf$+i+3)
Next i
Function Return ss ; repeat until valid data received
EndIf
Until ss<-1 ; otherwise repeat forever, unless timeout
Function Return ss
End Function
Function waitConnection{}
; return -2 (#TIMEOUT) if timeout
SHARED s,r
timeout.l=False
buf$=String$(" ",2048)
lr$=Chr$(23)+Chr$(1)+Chr$(2)+Chr$(1)+Chr$(6)+Chr$(1)+Chr$(0)+Chr$(0)+Chr$(0)+Chr$(0)+Chr$(255)+Chr$(2)+Chr$(1)+Chr$(2)
lr$=lr$+Chr$(3)+Chr$(1)+Chr$(1)+Chr$(4)+Chr$(2)+Chr$(64)+Chr$(0)+Chr$(8)+Chr$(1)+Chr$(3)
Repeat
dummy.l=read_frame{&buf$}
Until (dummy>0) OR (dummy<-1)
If dummy<-1 ; usually if `-2' i.e. TIMEOUT
Function Return dummy
Else
send_frame{&lr$,Len(lr$)}
EndIf
r=1
dummy.l=read_frame{&buf$}
s=1
Function Return dummy ; this value could be #TIMEOUT
End Function
Statement sendDisconnect{}
ld$=Chr$(7)+Chr$(2)+Chr$(1)+Chr$(1)+Chr$(255)+Chr$(2)+Chr$(1)+Chr$(0)
send_frame{&ld$,Len(ld$)}
End Statement